﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using s2b.ControleDesenvolvimentoProjetos.Util.Auxiliar;

namespace s2b.ControleDesenvolvimentoProjetos
{
    public partial class WUCStatus : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["ID_STATUS"] != null)
            {

                PreencheDados();
                btnStatus.Text = "Atualizar Status";

            }
            else
            {
                btnStatus.Text = "Adicionar Status";

            }

        }
        public void PreencheDados()
        {

            if (!IsPostBack)
            {

                SqlDataReader executa = Auxiliar.RetornaRegistros(String.Format("SELECT * FROM STATUS WHERE ID_CLIENTE = {0}", Request.QueryString["ID_STATUS"]), "STATUS");


                while (executa.Read())
                {
                    txtStatus.Text = executa["DESC_STATUS"].ToString();

                }
                Auxiliar.FecharConexao();


            }
        }
        public void InsertStatus()
        {
            try
            {
                Auxiliar.ExecutaComando(String.Format("INSERT INTO STATUS VALUES('{0}')", txtStatus.Text));
                Auxiliar.RetornaTabela(GridView1, "STATUS", "SELECT * FROM STATUS");
                lblMsg.Text = "Status adicionado com sucesso!";

            }
            catch (Exception)
            {
                lblMsg.Text = "Erro ao adicionar status!";
            }
        }
        public void UpdateStatus()
        {

            try
            {

                Auxiliar.ExecutaComando(String.Format("UPDATE STATUS SET DESC_STATUS = '{0}' WHERE ID_STATUS = {1}", txtStatus.Text, Request.QueryString["ID_STATUS"]));
                Auxiliar.RetornaTabela(GridView1, "STATUS", "SELECT * FROM STATUS");
                Response.Redirect("WFVisualizarStatus.aspx");

            }
            catch (Exception ex)
            {

                throw;
            }
        }
        protected void btnStatus_Click(object sender, EventArgs e)
        {
            if (Request.QueryString["ID_STATUS"] != null)
            {
                UpdateStatus();

            }
            else
            {

                InsertStatus();

            }
        }
    }
}